package com.antoco.operation.bigscreen.controller.modelcontroller.impl;

import com.antoco.operation.bigscreen.controller.modelcontroller.intf.ThisDaySuiziController;
import com.antoco.operation.bigscreen.model.dto.ThisDaySuiziDTO;
import com.antoco.operation.bigscreen.model.entity.ThisDaySuizi;
import com.antoco.operation.bigscreen.mapper.ThisDaySuiziMapper;
import com.antoco.operation.bigscreen.service.ChannelDataService;
import com.antoco.operation.bigscreen.service.autogeneration.intf.ThisDaySuiziService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/model/this-day-suizi")
@RestController
public class ThisDaySuiziControllerImpl implements ThisDaySuiziController {

    @Autowired
    private ThisDaySuiziService thisDaySuiziService;

    @Autowired
    private ThisDaySuiziMapper thisDaySuiziMapper;

    @Autowired
    private ChannelDataService channelDataService;

    @Override
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public ThisDaySuiziDTO save(@Validated @RequestBody ThisDaySuiziDTO thisDaySuiziDTO) {
        ThisDaySuizi thisDaySuizi = thisDaySuiziMapper.asEntity(thisDaySuiziDTO);
        thisDaySuiziService.save(thisDaySuizi);
        channelDataService.pushAllLists(this.getClass().getName());
        return thisDaySuiziDTO;
    }

    @Override
    @GetMapping("/{id}")
    public ThisDaySuiziDTO findById(@PathVariable("id") String id) {
        ThisDaySuizi thisDaySuizi = thisDaySuiziService.findById(id).orElse(null);
        return thisDaySuiziMapper.asDTO(thisDaySuizi);
    }

    @Override
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") String id) {
        thisDaySuiziService.deleteById(id);
        channelDataService.pushAllLists(this.getClass().getName());
    }

    @Override
    @GetMapping
    public List<ThisDaySuiziDTO> list() {
        return thisDaySuiziMapper.asDTOList(thisDaySuiziService.findAll());
    }

    @Override
    @GetMapping("/page-query")
    public Page<ThisDaySuiziDTO> pageQuery(Pageable pageable) {
        Page<ThisDaySuizi> thisDaySuiziPage = thisDaySuiziService.findAll(pageable);
        List<ThisDaySuiziDTO> dtoList = thisDaySuiziPage
                .stream()
                .map(thisDaySuiziMapper::asDTO).collect(Collectors.toList());
        return new PageImpl<>(dtoList, pageable, thisDaySuiziPage.getTotalElements());
    }

    @Override
    @PutMapping("/{id}")
    public ThisDaySuiziDTO update(@RequestBody ThisDaySuiziDTO thisDaySuiziDTO, @PathVariable("id") String id) {
        ThisDaySuizi thisDaySuizi = thisDaySuiziMapper.asEntity(thisDaySuiziDTO);
        thisDaySuiziService.update(thisDaySuizi, id);
        channelDataService.pushAllLists(this.getClass().getName());
        return thisDaySuiziDTO;
    }
}